package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.JsonUtil;
import com.cz.nongyetongv5.bean.StayCheck;
import com.cz.nongyetongv5.bean.StayCheckBean;
import com.cz.nongyetongv5.bean.StayMark;
import com.cz.nongyetongv5.bean.StayMarkBean;
import com.cz.nongyetongv5.bean.User;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;


/**
 * 待办事项
 */

public class StayActivity extends Activity implements View.OnClickListener {

    private TextView tv_ent;
    private RelativeLayout rl_back;
    private StayCheck stayCheck;
    private TextView tv_ent_count;
    private TextView tv_patrol_count;
    private TextView tv_supervise_count;

    private LinearLayout ll_ent;
    private LinearLayout ll_patrol;
    private LinearLayout ll_supervise;
    private LinearLayout ll_user;
    private TextView tv_user_count;
    private TextView tv_user;


    private LinearLayout ll_register;
    private TextView tv_register_count;
    private TextView tv_register;


    private LinearLayout ll_archive;
    private TextView tv_archive_count;
    private TextView tv_archive;


    private TextView tv_patrol;

    private TextView tv_supervise;

    private User user;


    private StayCheckBean stayCheckBean;
    private StayMarkBean stayMarkBean;
    private StayMark stayMark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stay);
        //        stayCheck = (StayCheck) getIntent().getSerializableExtra("stayCheck");
        user = (User) getIntent().getSerializableExtra("user");
        rl_back = findViewById(R.id.rl_back);
        rl_back.setOnClickListener(this);
        tv_ent = findViewById(R.id.tv_ent);
        tv_ent.setOnClickListener(this);
        tv_patrol = findViewById(R.id.tv_patrol);
        tv_patrol.setOnClickListener(this);
        tv_supervise = findViewById(R.id.tv_supervise);
        tv_supervise.setOnClickListener(this);
        tv_ent_count = findViewById(R.id.tv_ent_count);
        tv_patrol_count = findViewById(R.id.tv_patrol_count);
        tv_supervise_count = findViewById(R.id.tv_supervise_count);

        ll_ent = findViewById(R.id.ll_ent);
        ll_patrol = findViewById(R.id.ll_patrol);
        ll_supervise = findViewById(R.id.ll_supervise);
        ll_ent.setOnClickListener(this);
        ll_patrol.setOnClickListener(this);
        ll_supervise.setOnClickListener(this);

        ll_user = findViewById(R.id.ll_user);
        ll_user.setOnClickListener(this);
        tv_user = findViewById(R.id.tv_user);
        tv_user_count = findViewById(R.id.tv_user_count);
        tv_user.setOnClickListener(this);


        ll_register = findViewById(R.id.ll_register);
        ll_register.setOnClickListener(this);
        tv_register = findViewById(R.id.tv_register);
        tv_register_count = findViewById(R.id.tv_register_count);
        tv_register.setOnClickListener(this);


        ll_archive = findViewById(R.id.ll_archive);
        ll_archive.setOnClickListener(this);
        tv_archive = findViewById(R.id.tv_archive);
        tv_archive_count = findViewById(R.id.tv_archive_count);
        tv_archive.setOnClickListener(this);


        //        tv_ent_count.setText("待审核(" + stayCheck.getEntCount() + "户)");
        //        tv_patrol_count.setText("待处理问题(" + stayCheck.getCheckCount() + "个)");
        //        tv_supervise_count.setText("待审核(" + stayCheck.getSuperviseCount() + "户)");
        //        if (stayCheck.getEntCount() == 0) {
        //            tv_ent.setVisibility(View.GONE);
        //        } else {
        //            tv_ent.setVisibility(View.VISIBLE);
        //        }
        //        if (stayCheck.getCheckCount() == 0) {
        //            tv_patrol.setVisibility(View.GONE);
        //        } else {
        //            tv_patrol.setVisibility(View.VISIBLE);
        //        }
        //        if (stayCheck.getSuperviseCount() == 0) {
        //            tv_supervise.setVisibility(View.GONE);
        //        } else {
        //            tv_supervise.setVisibility(View.VISIBLE);
        //        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_ent:
            case R.id.ll_ent:
                Intent intent = new Intent(StayActivity.this, EntListNewActivity.class);
//                intent.putExtra("add_user", user.getUsername());
                intent.putExtra("user", user);
//                intent.putExtra("region_code", user.getKnowRegion());
                startActivity(intent);
                break;
            case R.id.tv_patrol:
            case R.id.ll_patrol:
                Intent patrolIntent = new Intent(StayActivity.this, PatrolListNewActivity.class);
//                patrolIntent.putExtra("add_user", user.getUsername());
                //                patrolIntent.putExtra("regions", stayCheck.getRegions());
//                patrolIntent.putExtra("region_code", user.getKnowRegion());
                patrolIntent.putExtra("user", user);
                startActivity(patrolIntent);
                break;
            case R.id.tv_supervise:
            case R.id.ll_supervise:
                Intent superviseIntent = new Intent(StayActivity.this, SuperviseListNew2Activity.class);
//                superviseIntent.putExtra("add_user", user.getUsername());
                //                superviseIntent.putExtra("regions", stayCheck.getRegions());
//                superviseIntent.putExtra("region_code", user.getKnowRegion());
                superviseIntent.putExtra("user", user);
                startActivity(superviseIntent);
                break;
            case R.id.tv_user:
            case R.id.ll_user:
                Intent userIntent = new Intent(StayActivity.this, UserListActivity.class);
                userIntent.putExtra("add_user", user.getUsername());
                userIntent.putExtra("regions", stayCheck.getRegions());
                userIntent.putExtra("region_code", user.getKnowRegion());
                startActivity(userIntent);
                break;
            case R.id.tv_register:
            case R.id.ll_register:
                Intent registerIntent = new Intent(StayActivity.this, RegisterListActivity.class);
                registerIntent.putExtra("add_user", user.getUsername());
                registerIntent.putExtra("regions", stayCheck.getRegions());
                registerIntent.putExtra("region_code", user.getKnowRegion());
                startActivity(registerIntent);
                break;
            case R.id.tv_archive:
            case R.id.ll_archive:
                Intent archiveIntent = new Intent(StayActivity.this, ArchiveListActivity.class);
                archiveIntent.putExtra("add_user", user.getUsername());
                archiveIntent.putExtra("regions", stayCheck.getRegions());
                archiveIntent.putExtra("region_code", user.getKnowRegion());
                startActivity(archiveIntent);
                break;
            default:
                break;
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        getStay();
    }

    /**
     * 获取待办
     */
    void getStay() {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "system/getCheckMatter.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", user.getKnowRegion())
                .addParams("realname", user.getRealname())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(StayActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);
                        stayCheckBean = JsonUtil.toObject(response, StayCheckBean.class);
                        if (stayCheckBean.getCode().equals("0")) {
                            //                            getMark();
                            stayCheck = stayCheckBean.getData();
                            tv_ent_count.setText("待审核(" + stayCheck.getEntCount() + "户)");
                            tv_patrol_count.setText("待处理问题(" + stayCheck.getCheckCount() + "个)");
                            tv_supervise_count.setText("待审核(" + stayCheck.getSuperviseCount() + "户)");
                            if (stayCheck.getEntCount() == 0) {
                                tv_ent.setVisibility(View.GONE);
                                ll_ent.setClickable(false);
                            } else {
                                tv_ent.setVisibility(View.VISIBLE);
                                ll_ent.setClickable(true);
                            }
                            if (stayCheck.getCheckCount() == 0) {
                                tv_patrol.setVisibility(View.GONE);
                                ll_patrol.setClickable(false);
                            } else {
                                tv_patrol.setVisibility(View.VISIBLE);
                                ll_patrol.setClickable(true);
                            }
                            if (stayCheck.getSuperviseCount() == 0) {
                                tv_supervise.setVisibility(View.GONE);
                                ll_supervise.setClickable(false);
                            } else {
                                tv_supervise.setVisibility(View.VISIBLE);
                                ll_supervise.setClickable(true);
                            }
                        } else {
                            Toast.makeText(StayActivity.this, stayCheckBean.getMesssage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    void getMark() {
        OkHttpUtils
                .get()
                .url(InitApplication.markUrl + "system/getMarkCount.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("regions", stayCheckBean.getData().getRegions())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        //                        Toast.makeText(getActivity(), "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);
                        stayMarkBean = JsonUtil.toObject(response, StayMarkBean.class);
                        if (stayMarkBean.getCode().equals("0")) {
                            stayMark = stayMarkBean.getData();
                            tv_user_count.setText("待审核(" + stayMark.getUserCount() + "个)");
                            tv_register_count.setText("待审核(" + stayMark.getRegisterCount() + "家)");
                            tv_archive_count.setText("待审核(" + stayMark.getEntCount() + "家)");
                            if (stayMark.getUserCount() == 0) {
                                tv_user.setVisibility(View.GONE);
                                ll_user.setClickable(false);
                            } else {
                                tv_user.setVisibility(View.VISIBLE);
                                ll_user.setClickable(true);
                            }
                            if (stayMark.getRegisterCount() == 0) {
                                tv_register.setVisibility(View.GONE);
                                ll_register.setClickable(false);
                            } else {
                                tv_register.setVisibility(View.VISIBLE);
                                ll_register.setClickable(true);
                            }
                            if (stayMark.getEntCount() == 0) {
                                tv_archive.setVisibility(View.GONE);
                                ll_archive.setClickable(false);
                            } else {
                                tv_archive.setVisibility(View.VISIBLE);
                                ll_archive.setClickable(true);
                            }
                        } else {
                            //                            Toast.makeText(getActivity(), stayCheckBean.getMesssage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

}
